define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/index' + location.search,
                    edit_url: 'course/edit',
                    del_url: 'course/del',
                    multi_url: 'course/multi',
                    table: 'course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                searchFormVisible: true,
                searchFormTemplate: 'course_form_tpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'brand.name', title: __('Brand'), operate: false},
                        {field: 'series.name', title: __('course_series_id')},
                        {
                            field: 'age_range', title: __('Age_range'), formatter: function (value, row, index) {
                                let age_range = "<div style='text-align:center;'>" + row.min_age;
                                if (row.min_age_unit == 1) {
                                    age_range += '个月';
                                } else {
                                    age_range += '年';
                                }
                                age_range += '-' + row.max_age;
                                if (row.max_age_unit == 1) {
                                    age_range += '个月';
                                } else {
                                    age_range += '年';
                                }
                                age_range += "</div>";
                                return age_range;
                            }, operate: false
                        },
                        {field: 'class_hour', title: __('Class_hour')},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __("Status 0"), "1": __("Status 1")},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'ware', title: __('Ware'), formatter: function (value, row, index) {
                                var ret = "<div style='text-align:center;'>";
                                if (row.ware) {
                                    ret += '√';
                                } else {
                                    ret += '×';
                                }
                                ret += '</div>';
                                return ret;
                            }, operate: false
                        },
                        {
                            field: 'practice', title: __('Practice'), formatter: function (value, row, index) {
                                var ret = "<div style='text-align:center;'>";
                                if (row.practice) {
                                    ret += '√';
                                } else {
                                    ret += '×';
                                }
                                ret += '</div>';
                                return ret;
                            }, operate: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('编辑'),
                                    text: __('编辑'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    url: 'course/edit',
                                    success: function (data, ret) {
                                        $('a.btn-refresh').trigger("click")
                                    },
                                },
                                {
                                    name: 'del',
                                    title: __('删除'),
                                    text: __('删除'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    confirm: __('Are you sure you want to delete this item?'),
                                    url: 'course/del',
                                    success: function (data, ret) {
                                        $('a.btn-refresh').trigger("click")
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });
            $(document).on('click', '.btn-add_brand', function () {
                layer.open({
                    type: 2,
                    closeBtn: 1,
                    title: "添加品牌",
                    area: ['80%', '80%'],
                    content: $(this).attr('data-url'),
                    success: function (layero, index) {
                    }
                });
            });
            $(document).on('click', '.btn-course_config', function () {
                layer.open({
                    type: 2,
                    closeBtn: 1,
                    title: "规则设置",
                    area: ['80%', '80%'],
                    content: $(this).attr('data-url'),
                    success: function (layero, index) {
                    }
                });
            });
            $(document).on('click', '.btn-add_course', function () {
                layer.open({
                    type: 2,
                    closeBtn: 1,
                    title: "添加课程",
                    area: ['80%', '80%'],
                    content: $(this).attr('data-url'),
                    success: function (layero, index) {
                    }
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
